package com.ygsoft.tianlu_mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygsoft.tianlu_mall.pojo.Message;
import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("mes")
public class messageController {
    @Autowired
    MessageService messageService;
    //查询所有信息=0  所有未读消息=1  所有物流消息=2  所有系统消息=3  未读物流消息=4
    // 未读系统消息=5  所有已读消息=6  已读物流信息=7  已读系统信息=8
    @RequestMapping(value = "/Messages",method = RequestMethod.GET)
    @ResponseBody
    public Msg getMessageById(
            @RequestParam(value = "message_userid") int message_userid,
            @RequestParam(value = "pn",defaultValue = "1")Integer pn,
            @RequestParam Integer pageSize,
            @RequestParam(value = "messageNumber") int messageNumber) {
        try {
            PageHelper.startPage(pn, pageSize);
            int me = messageNumber;
            switch (me) {
                case 0:
                    List<Message> messages = messageService.getMessageById(message_userid);
                    PageInfo page = new PageInfo(messages, 4);
                    return Msg.succes().add("MessageInfo", page);
                case 1:
                    List<Message> message1 = messageService.getUnreadMessages(message_userid);
                    PageInfo page1 = new PageInfo(message1, 4);
                    return Msg.succes().add("MessageInfo", page1);
                case 2:
                    List<Message> message2 = messageService.getlogisticsMessages(message_userid);
                    PageInfo page2 = new PageInfo(message2, 4);
                    return Msg.succes().add("MessageInfo", page2);
                case 3:
                    List<Message> message3 = messageService.getSystemMessages(message_userid);
                    PageInfo page3 = new PageInfo(message3, 4);
                    return Msg.succes().add("MessageInfo", page3);
                case 4:
                    List<Message> message4 = messageService.getunreadlogisticsMessages(message_userid);
                    PageInfo page4 = new PageInfo(message4,4);
                    return  Msg.succes().add("MessageInfo",page4);
                case 5:
                    List<Message> message5 = messageService.getunreadSystemMessages(message_userid);
                    PageInfo page5 = new PageInfo(message5,4);
                    return  Msg.succes().add("MessageInfo",page5);
                case 6:
                    List<Message> message6 = messageService.getreadMessages(message_userid);
                    PageInfo page6 = new PageInfo(message6,4);
                    return  Msg.succes().add("MessageInfo",page6);
                case 7:
                    List<Message> message7 = messageService.getreadlogisticsMessages(message_userid);
                    PageInfo page7 = new PageInfo(message7,4);
                    return  Msg.succes().add("MessageInfo",page7);
                case 8:
                    List<Message> message8 = messageService.getreadSystemMessages(message_userid);
                    PageInfo page8 = new PageInfo(message8,4);
                    return  Msg.succes().add("MessageInfo",page8);
                default:
                    System.out.println("参数有误");

            }
            return Msg.succes();
        }    catch (Exception e){
        e.printStackTrace();
        return Msg.fail();
    }
    }

    //删除某条信息
    @RequestMapping(value = "/deleteMessage/{message_id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteMessage(@PathVariable("message_id")int message_id){
        messageService.deleteMessage(message_id);
        return Msg.succes();
    }
    //批量删除
    @RequestMapping(value = "/deletemoreMessage",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deletemoreMessage(@RequestParam List<Integer> message_idList) {
        try {
            for (int message_id:message_idList) {
                deleteMessage(message_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
    //消息已读
    @RequestMapping(value = "/setMessageRead",method = RequestMethod.PUT)
    @ResponseBody
    public Msg setMessageRead(@RequestParam(value = "message_id") int message_id){
        messageService.setMessageRead(message_id);
        return Msg.succes();
    }
    //批量已读
    @RequestMapping(value = "/setMoreMessageRead",method = RequestMethod.PUT)
    @ResponseBody
    public Msg setMoreMessageRead(@RequestParam List<Integer> message_idList){
        try {
            for (int message_id:message_idList) {
                setMessageRead(message_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
    //未读消息数量
    @RequestMapping(value = "/getMessageCount",method = RequestMethod.GET)
    @ResponseBody
    public Msg getMessageCount(@RequestParam(value = "message_userid") int message_userid  ){
        try {
            int count = messageService.getCartCount(message_userid);
            Map<String, Object> result = new HashMap<>();
            result.put("count",count);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
//    //所有未读消息
//    @RequestMapping(value = "/unreadMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getUnreadMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getUnreadMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
//    //所有物流消息
//    @RequestMapping(value = "/logisticsMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getlogisticsMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getlogisticsMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
//    //所有系统消息
//    @RequestMapping(value = "/SystemMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getSystemMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getSystemMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
//
//    //未读物流消息
//    @RequestMapping(value = "/unreadlogisticsMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getunreadlogisticsMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getunreadlogisticsMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
//    //未读系统消息
//    @RequestMapping(value = "/unreadSystemMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getunreadSystemMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getunreadSystemMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
//    //所有已读消息
//    @RequestMapping(value = "/readMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getreadMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getreadMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("readMessages",page);
//    }
//    //已读物流信息
//    @RequestMapping(value = "/readlogisticsMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getreadlogisticsMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getreadlogisticsMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("readlogisticsMessages",page);
//    }
//    //已读系统信息
//    @RequestMapping(value = "/readSystemMessages",method = RequestMethod.GET)
//    @ResponseBody
//    public Msg getreadSystemMessages(
//            @RequestParam(value = "message_userid") int message_userid,@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,4);
//        List<Message> messages = messageService.getreadSystemMessages(message_userid);
//        PageInfo page = new PageInfo(messages,4);
//        return  Msg.succes().add("pageInfo",page);
//    }
}
